package br.com.busgpsapp.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import br.com.busgpsapp.R;
import br.com.busgpsapp.util.Constants;

import com.actionbarsherlock.app.SherlockActivity;

public class SplashActivity extends SherlockActivity implements Runnable
{

    private Context ctx;

    private static long SLEEP_TIME = 3000;

    private String mostSearched;

    private String lastSearched;

    private SharedPreferences sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_splash_screen);

        ctx = this;

        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);

        mostSearched = sharedPrefs.getString(Constants.MOST_SEARCHED, null);

        lastSearched = sharedPrefs.getString(Constants.MOST_SEARCHED, null);

        new Handler().postDelayed(this, SLEEP_TIME);
    }

    @Override
    public void run()
    {
        Intent it = null;

        Boolean isFirstTime = Boolean.parseBoolean(sharedPrefs.getString(Constants.FIRST_TIME, null));
        
//        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        
        if (isFirstTime)
        {
//            TODO - Marcar como ja utilizado.
            it = new Intent(this, TutorialActivity.class);
        }
        else
        {
            it = new Intent(this, MainActivity.class);

            if (mostSearched != null)
            {
                it.putExtra(Constants.MOST_SEARCHED, mostSearched);
            }
            if (lastSearched != null)
            {
                it.putExtra(Constants.LAST_SEARCHED, lastSearched);
            }
        }
        startActivity(it);
        finish();
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }
}
